package com.ruoyi.dataserver.point.utils;



import com.ruoyi.dataserver.line.enums.OperationSymbolEnum;
import com.ruoyi.dataserver.point.dto.PointFieldAndOperationSymbolAndValue;
import com.ruoyi.dataserver.point.enums.PointFieldEnum;

import java.util.List;


/**
 * @ProjectName intelligent-city
 * @Package com.lab.dataserver.point.utils
 * @ClassName PointSqlUtil
 * @Description 附属物sql生成工具类
 * @Company lab
 * @Author zgq
 * @Date 2020/7/17 13:49
 * @UpdateUser
 * @UpdateDate
 * @UpdateRemark
 * @Version 1.0.0
 */
public class PointSqlUtil {

    /******************************横断面分析查询所用接口start******************************/

    public static String selectSql(String tableName, List<PointFieldAndOperationSymbolAndValue> povList) {
        String sql = "select * from \"" + tableName + "\" t" + whereSql(povList);
        return sql;
    }

    private static String whereSql(List<PointFieldAndOperationSymbolAndValue> povList) {
        String sql = "";
        for (int i = 0; i < povList.size(); i++) {
            PointFieldAndOperationSymbolAndValue pov = povList.get(i);
            PointFieldEnum pointField = PointFieldEnum.getPointFieldEnumWithCode(pov.getPointField());
            OperationSymbolEnum operationSymbol = OperationSymbolEnum.getOperationSymbolEnumWithCode(pov.getOperationSymbol());
            String value = pov.getValue();
            if (OperationSymbolEnum.LIKE == operationSymbol) {
                value = "%" + value + "%";
            }
            if (i == 0) {
                sql += " WHERE " + pointField.getFieldName() + " " + operationSymbol.getOperationSymbol() + " '" + value + "'";
            } else {
                sql += " OR " + pointField.getFieldName() + " " + operationSymbol.getOperationSymbol() + " '" + value + "'";
            }
        }
        return sql;
    }
    /******************************横断面分析查询所用接口end******************************/
}
